<template>
  <div>
    <el-dialog title="添加核查计划" :visible.sync="dialogVisible">
      <el-form labelWidth="150px">
        <el-form-item label="选择设备" style="width:100%" class="is-required">
          <selecteq :selecteqid.sync="selecteqid" :itempersonid="selecteqid" :disabled="chousebtn"></selecteq>
          <div class="el-form-item__error" v-if="!$v.selecteqid.id.required">
            请选择设备
          </div>
        </el-form-item>
        <el-form-item label="本次检定/校准日期" style="width:100%" class="is-required">
          <el-date-picker
            v-model="selecteqid.verifiytime"
            type="date"
            style="width:100%"
            placeholder="选择日期"
            disabled
          ></el-date-picker>
          <div class="el-form-item__error" v-if="!$v.selecteqid.verifiytime.required">
            请选择本次检定/校准日期
          </div>
        </el-form-item>
        <el-form-item label="核查实施时间" style="width:100%" class="is-required">
          <el-date-picker
            v-model="selecteqid.check_time"
            type="date"
            style="width:100%"
            placeholder="选择日期"
            valueFormat="yyyy-MM-dd"
          ></el-date-picker>
          <div class="el-form-item__error" v-if="!$v.selecteqid.check_time.required">
            请选择核查实施时间
          </div>
        </el-form-item>
        <el-form-item>
          <div>&nbsp;</div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addcheckall">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import selecteq from '~/components/common/selecteq.vue';
import { required } from 'vuelidate/lib/validators';

export default {
  validations: {
    selecteqid: {
      verifiytime: {
        required,
      },
      check_time: {
        required,
      },
      id: {
        required,
      },
    },
  },
  components: {
    selecteq,
  },
  props: {
    itemvalname: Object,
    chousebtn: Number,
  },
  data() {
    return {
      selecteqid: {}, // 选择设备
      addcheckdate: '', // 核查实施时间
      dialogVisible: true,
      ts: this.newStore(),
    };
  },
  created() {
    this.getcarval();
  },
  methods: {
    // 渲染信息
    getcarval() {
      // console.log(this.itemvalname, '222');
      if (this.itemvalname == '' || this.itemvalname == undefined) {
      } else if (this.chousebtn == 2) {
        this.selecteqid = {};
        // console.log(this.itemvalname, '1');
        // console.log(this.branchidlist, "2");
        this.selecteqid = this.itemvalname.hash;

        // console.log(this.selecteqid.eid, '3');
      }
    },
    // 添加
    addcheckall() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else if (this.chousebtn == 1) {
        this.$confirm('是否添加核查计划?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const req = {
              handle:
                'DD5D108B99FC12B7BAB3E6EAE5C936EF5FBB6F4BAE9BBB7375CF9C8A35D44EF2',
              eid: this.selecteqid.id,
              check_time: this.selecteqid.check_time,
            };
            this.spost(this.ts, '/mainservlet', req).then((ts) => {
              if (ts.d.sflag == 'success') {
                this.$message({
                  type: 'success',
                  message: '添加成功!',
                });
                this.dialogVisible = false;
                this.$parent.getperiolist();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消',
            });
          });
      } else if (this.chousebtn == 2) {
        this.$confirm('是否修改核查计划?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const req = {
              handle:
                'DD5D108B99FC12B7BAB3E6EAE5C936EF5FBB6F4BAE9BBB738A8DD8AA772C6F4B',
              id: this.selecteqid.id,
              check_time: this.selecteqid.check_time,
            };
            this.spost(this.ts, '/mainservlet', req).then((ts) => {
              if (ts.d.sflag == 'success') {
                this.$message({
                  type: 'success',
                  message: '修改成功!',
                });
                this.dialogVisible = false;
                this.$parent.getperiolist();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消',
            });
          });
      }
    },
  },
};
</script>


<style scoped>
.el-form-item {
  width: 50%;
  margin-bottom: 22px;
  float: left;
}
.statusspan {
  display: inline-block;
  width: 30px;
  height: 30px;
}
</style>
